package com.netas.kekmanager.dao.impl;

import com.netas.kekmanager.dao.CommentLikeDao;
import com.netas.kekmanager.dao.model.CommentLike;
import com.netas.kekmanager.dao.model.CommentLikePk;
import com.netas.kekmanager.dao.exceptions.CommentLikeDaoException;
import java.util.List;
import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.transaction.annotation.Transactional;

public class CommentLikeDaoImpl extends AbstractDAO implements ParameterizedRowMapper<CommentLike>, CommentLikeDao
{
	protected SimpleJdbcTemplate jdbcTemplate;

	protected DataSource dataSource;

	/**
	 * Method 'setDataSource'
	 * 
	 * @param dataSource
	 */
	public void setDataSource(DataSource dataSource)
	{
		this.dataSource = dataSource;
		jdbcTemplate = new SimpleJdbcTemplate(dataSource);
	}

	/**
	 * Method 'insert'
	 * 
	 * @param dto
	 * @return CommentLikePk
	 */
	@Transactional
	public CommentLikePk insert(CommentLike dto)
	{
		jdbcTemplate.update("INSERT INTO " + getTableName() + " ( comment_id, user_id ) VALUES ( ?, ? )",dto.getCommentId(),dto.getUserId());
		CommentLikePk pk = new CommentLikePk();
		pk.setId( jdbcTemplate.queryForInt("select last_insert_id()") );
		return pk;
	}

	/** 
	 * Updates a single row in the comment_like table.
	 */
	@Transactional
	public void update(CommentLikePk pk, CommentLike dto) throws CommentLikeDaoException
	{
		jdbcTemplate.update("UPDATE " + getTableName() + " SET id = ?, comment_id = ?, user_id = ? WHERE id = ?",dto.getId(),dto.getCommentId(),dto.getUserId(),pk.getId());
	}

	/** 
	 * Deletes a single row in the comment_like table.
	 */
	@Transactional
	public void delete(CommentLikePk pk) throws CommentLikeDaoException
	{
		jdbcTemplate.update("DELETE FROM " + getTableName() + " WHERE id = ?",pk.getId());
	}

	/**
	 * Method 'mapRow'
	 * 
	 * @param rs
	 * @param row
	 * @throws SQLException
	 * @return CommentLike
	 */
	public CommentLike mapRow(ResultSet rs, int row) throws SQLException
	{
		CommentLike dto = new CommentLike();
		dto.setId( rs.getInt( 1 ) );
		dto.setCommentId( rs.getInt( 2 ) );
		dto.setUserId( rs.getInt( 3 ) );
		return dto;
	}

	/**
	 * Method 'getTableName'
	 * 
	 * @return String
	 */
	public String getTableName()
	{
		return "spring2013g3.comment_like";
	}

	/** 
	 * Returns all rows from the comment_like table that match the criteria 'id = :id'.
	 */
	@Transactional
	public CommentLike findByPrimaryKey(int id) throws CommentLikeDaoException
	{
		try {
			List<CommentLike> list = jdbcTemplate.query("SELECT id, comment_id, user_id FROM " + getTableName() + " WHERE id = ?", this,id);
			return list.size() == 0 ? null : list.get(0);
		}
		catch (Exception e) {
			throw new CommentLikeDaoException("Query failed", e);
		}
		
	}

	/** 
	 * Returns all rows from the comment_like table that match the criteria ''.
	 */
	@Transactional
	public List<CommentLike> findAll() throws CommentLikeDaoException
	{
		try {
			return jdbcTemplate.query("SELECT id, comment_id, user_id FROM " + getTableName() + " ORDER BY id", this);
		}
		catch (Exception e) {
			throw new CommentLikeDaoException("Query failed", e);
		}
		
	}

	/** 
	 * Returns all rows from the comment_like table that match the criteria 'comment_id = :commentId'.
	 */
	@Transactional
	public List<CommentLike> findByComment(int commentId) throws CommentLikeDaoException
	{
		try {
			return jdbcTemplate.query("SELECT id, comment_id, user_id FROM " + getTableName() + " WHERE comment_id = ?", this,commentId);
		}
		catch (Exception e) {
			throw new CommentLikeDaoException("Query failed", e);
		}
		
	}

	/** 
	 * Returns all rows from the comment_like table that match the criteria 'user_id = :userId'.
	 */
	@Transactional
	public List<CommentLike> findByUser(int userId) throws CommentLikeDaoException
	{
		try {
			return jdbcTemplate.query("SELECT id, comment_id, user_id FROM " + getTableName() + " WHERE user_id = ?", this,userId);
		}
		catch (Exception e) {
			throw new CommentLikeDaoException("Query failed", e);
		}
		
	}

	/** 
	 * Returns all rows from the comment_like table that match the criteria 'id = :id'.
	 */
	@Transactional
	public List<CommentLike> findWhereIdEquals(int id) throws CommentLikeDaoException
	{
		try {
			return jdbcTemplate.query("SELECT id, comment_id, user_id FROM " + getTableName() + " WHERE id = ? ORDER BY id", this,id);
		}
		catch (Exception e) {
			throw new CommentLikeDaoException("Query failed", e);
		}
		
	}

	/** 
	 * Returns all rows from the comment_like table that match the criteria 'comment_id = :commentId'.
	 */
	@Transactional
	public List<CommentLike> findWhereCommentIdEquals(int commentId) throws CommentLikeDaoException
	{
		try {
			return jdbcTemplate.query("SELECT id, comment_id, user_id FROM " + getTableName() + " WHERE comment_id = ? ORDER BY comment_id", this,commentId);
		}
		catch (Exception e) {
			throw new CommentLikeDaoException("Query failed", e);
		}
		
	}

	/** 
	 * Returns all rows from the comment_like table that match the criteria 'user_id = :userId'.
	 */
	@Transactional
	public List<CommentLike> findWhereUserIdEquals(int userId) throws CommentLikeDaoException
	{
		try {
			return jdbcTemplate.query("SELECT id, comment_id, user_id FROM " + getTableName() + " WHERE user_id = ? ORDER BY user_id", this,userId);
		}
		catch (Exception e) {
			throw new CommentLikeDaoException("Query failed", e);
		}
		
	}

	/** 
	 * Returns the rows from the comment_like table that matches the specified primary-key value.
	 */
	public CommentLike findByPrimaryKey(CommentLikePk pk) throws CommentLikeDaoException
	{
		return findByPrimaryKey( pk.getId() );
	}

        @Override
        public int getLikeCount(int commentID) throws CommentLikeDaoException {
            return jdbcTemplate.queryForInt("SELECT  count(*) FROM " + getTableName() + " WHERE comment_id = ? ", commentID);
        }

        @Override
        public int getUserLikeCountForComment(int userID, int commentID) throws CommentLikeDaoException {
            return jdbcTemplate.queryForInt("SELECT  count(*) FROM " + getTableName() + " WHERE user_id = ? AND comment_id = ? ", userID, commentID);
        }

        @Override
        public List<CommentLike> findWhereCommentIdAndUserIdEquals(int userID, int commentID) throws CommentLikeDaoException {
                try {
			return jdbcTemplate.query("SELECT id, comment_id, user_id FROM " + getTableName() + " WHERE user_id = ? AND comment_id = ? ", this,userID, commentID);
		}
		catch (Exception e) {
			throw new CommentLikeDaoException("Query failed", e);
		}
        }

}
